package com.finance.cooperate.feature.core.service.impl;

import com.finance.cooperate.common.properties.CooperateProperties;
import com.finance.cooperate.common.properties.EnvironmentProperties;
import com.finance.cooperate.common.utils.StringUtil;
import com.finance.cooperate.dao.entity.secondary.FeatureOneHot;
import com.finance.cooperate.dao.repository.secondary.FeatureOneHotRepository;
import com.finance.cooperate.feature.core.feature.FeatureOneHotType;
import com.finance.cooperate.feature.core.service.FeatureOneHotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName FeatureOneHotServiceImpl
 * @Description TODO
 * @Author shen
 * @Date 2022/5/25 16:34
 * @Modify ...
 */
@Slf4j
@Service
public class FeatureOneHotServiceImpl implements FeatureOneHotService {

    public static final String CACHE_NAME = "oneHot";

    @Autowired
    private FeatureOneHotRepository oneHotRepository;
    @Autowired
    private CooperateProperties cooperateProperties;

    @Cacheable(value = CACHE_NAME, key = "'key_'+#type+'_'+#value", unless = "#result==null")
//    @Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED)
    @Override
    public Long getOneHotCode(int type, String value) {


        // value为空表示缺失值
        if (StringUtil.isEmptyOrNvl(value)) {
            return null;
        }

        // 去除一些特殊字符
        value = value
                .replace("\"", "")
                .replace("'", "");

        Long code = 1L;

        FeatureOneHot example = new FeatureOneHot();
        example.setType(type);
        example.setValue(value.trim().toLowerCase());
        example.setProject(cooperateProperties.getEnvironment().getProject());

        Optional<FeatureOneHot> one = oneHotRepository.findOne(Example.of(example));

        if (one.isPresent()){//有编码，直接返回

            FeatureOneHot oneHot = one.get();
            code = oneHot.getCode();

        }else {
            //判断是否要进行自动添加编码，
            EnvironmentProperties environmentProperties = cooperateProperties.getEnvironment();

            if (!environmentProperties.getIsDebug()) {//非debug环境，不自动添加编码

                return null;
            }
            if (!environmentProperties.getIsOneHot()) {//设置不自动编码
                return null;
            }
            if (!FeatureOneHotType.getStatus(type)) {//该类别不自动编码
                return null;
            }

            FeatureOneHot oneHot = oneHotRepository.findFirstByTypeAndProjectOrderByCodeDesc(type, cooperateProperties.getEnvironment().getProject());

            if (oneHot != null) {
                code = oneHot.getCode() + 1L;
            }

            FeatureOneHot hot = new FeatureOneHot();
            hot.setType(type);
            hot.setValue(value.trim().toLowerCase());
            hot.setCode(code);
            hot.setProject(cooperateProperties.getEnvironment().getProject());

            oneHotRepository.save(hot);
        }

        return code;
    }

    @Cacheable(value = CACHE_NAME, key = "'key_'+#type", unless = "#result==null")
    @Override
    public List<Long> getOneHotList(int type) {

        List<Long> result = new ArrayList<>();

        FeatureOneHot example = new FeatureOneHot();
        example.setType(type);
        example.setProject(cooperateProperties.getEnvironment().getProject());

        List<FeatureOneHot> hots = oneHotRepository.findAll(Example.of(example));

        for (FeatureOneHot hot : hots) {
            result.add(hot.getCode());
        }

        return result;
    }

}
